<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter :title="title" width="600px" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { defineEmits, ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { vipcardCopyUseAirportFormSchema } from '../data/data';
import { copyVipCardSetAirport } from '../data/api';

const isUpdate = ref(true);
const emit = defineEmits(['register', 'success']);
const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
  labelWidth: 150,
  labelCol: { span: 6 },
  schemas: vipcardCopyUseAirportFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

const title = ref('');
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  const record = data.record;
  title.value = record.goodsName + '-复制卡种使用机场';
  if (unref(isUpdate)) {
    setFieldsValue({
      ...record,
    });
  }
});

async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    await copyVipCardSetAirport(values);
    console.log(values);
    closeDrawer();
    emit('success');
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
